package demo.practice.leetcode_base;

public class P26 {

    //第二种解法

    public static void main(String[] args) {
        int[] t = {1, 1};

        P26 p26 = new P26();
        p26.removeDuplicates(t);
    }

    //[1,1,2]
    //[1,2,2]

    public int removeDuplicates(int[] nums) {

        int length = nums.length;
        int count = 0;

        for (int i = 1; i + count < length; i++) {
            if (nums[i + count] == nums[i - 1]) {
                count++;    //删除的元素个数增加了
                if (i + count < length)
                    nums[i] = nums[i + count];
                i--;        //删除了元素，则需要再做一次
                continue;
            }
            if (count > 0) {    //当前元素被后面的元素代替
                nums[i] = nums[i + count];
            }
        }
        return length - count;
    }
}
